<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html;charset=utf-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>User Index</title>
 	<%@ include file="/common/view/header.jsp"%>
  	<script>
  		var grid=null, win_add=null, win_upt=null, form_add=null, form_upt=null, form=null;
  		/* 验证两次密码输入是否正确*/
  		$.extend($.fn.validatebox.defaults.rules, {  
		    equals: {  
		        validator: function(value,param){  
		            return value == $(param[0]).val();  
		        },  
		        message: '两次密码输入不一致。'  
		    }  
		});
  		$(function(){
  			//create datagrid
			grid = $('#tt').datagrid({
                url:'<%=cp%>/rbac/getPagesTUser',
                pageSize:20,
    			pageList:[5,10,15,20],
    			loadMsg:'正在加载系统用户列表......',
    			pagination:true,
    			method:"post",
				height: document.body.clientHeight,
				rownumbers:true,
                fitColumns: true,
                singleSelect: true,
                remoteSort:false,
                idField:'id',
                columns:[[
                	{title:'用户编号',field:'userno',width:fixWidth(0.1),sortable:true},
			        {title:'用户登录名',field:'username',width:fixWidth(0.16),sortable:false,align:'center'},
                	{title:'单位',field:'sectionname',width:fixWidth(0.15),sortable:false,align:'center',
                		formatter:function(value,row,index){
                			return row.section.name;
                		}
                	},
                	{title:'角色',field:'rolename',width:fixWidth(0.15),sortable:false,align:'center',
                		formatter:function(value,row,index){
                			return row.userRoles.length>0 ? row.userRoles[0].comments : "--";
                		}
                	},
			        {title:'最近登录IP',field:'last_loginip',width:fixWidth(0.16),sortable:false,align:'center'},
                	{title:'最近登录时间',field:'last_logintime',width:fixWidth(0.16),sortable:true,align:'center'},
                	{title:'注册时间',field:'reg_time',width:fixWidth(0.16),sortable:false,align:'center'},
                	{title:'状态',field:'status',width:fixWidth(0.08),sortable:false,align:'center',
                		formatter:function(value,row,index){
                			if(value=="0"){
                				return "<span style='color:#E10900'>已禁用</span>";
                			}
                			return "<span style='color:#009900'>已启用</span>";
                		}
                	},
                	{title:'操作',field:'doaction',align:'center',width:fixWidth(0.1),align:'center',
						formatter:function(value,row,index){
							var e = '<a href="###" onclick="editDataItem('+index+')">编辑</a> ';
							var d = '';
							if(row.status=='0'){
								d = '<a href="###" onclick="setupStatus(\''+index+'\',1)">启用</a>';
							}else if(row.status=='1'){
								d = '<a href="###" onclick="setupStatus(\''+index+'\',0)">禁用</a>';
							}
							return e+d;
						}
					}
				]],
                onDblClickRow:function(rowIndex, rowData){
                	editDataItem(rowIndex);
                },
				toolbar:[{
						id:'btnadd',
						text:'新增用户',
						iconCls:'icon-add',
						handler:newDataItem
					},'-',{
						text:'高级查询',
						iconCls:'icon-search',
						handler:openSearchForm
					},'-',{
						text:'显示全部',
						iconCls:'icon-reload',
						handler:displayAll
					},'-'
				]
            });
  			
  			//create searchbox to toolbar
  			//$('.datagrid-toolbar table tr').append("<td>"+$('.searchbox').html()+"</td>");
  			$("<td></td>").appendTo('.datagrid-toolbar table tr');
  			$('.searchbox').appendTo('.datagrid-toolbar table td:last');
  			
			//create window
			init_window();
			//create form
			init_form();
		});
  		/* 初始化窗口控件 */
  		function init_window(){
  			var width = 580;
  			var height = 400;
  			var top = (document.body.clientHeight-height)/2;
  			var left = (document.body.clientWidth-width)/2;
  			win_add = $('#userform').window({
				closed:true,
				width: width,
				height: height,
				top: top,
  				left: left,
				modal: true,
				iconCls:'icon-vcard'
			});
  			win_upt = $('#upt_userinfo_win').window({
				closed:true,
				width: width,
				height: height,
				top: top,
  				left: left,
				modal: true,
				iconCls:'icon-vcard'
			});
  		}
  		/* 初始化表单控件 */
  		function init_form(){
  			form_add = win_add.find('form');
  			form_upt = win_upt.find('form');
  		}
  		/* 新增数据项 */
  		function newDataItem(){
  			win_add.window('setTitle','添加新用户');
  			win_add.window('open');
			form_add.form('clear');
			form_add.url = '<%=cp%>/rbac/addUser';
			form = form_add;
  		}
  		/* 编辑数据项 */
  		function editDataItem(index){
  			var row = grid.datagrid('getRows')[index];
  			if (row){
  				win_upt.window('setTitle','更新用户资料');
				win_upt.window('open');
				form_upt.form('clear');
				form_upt.form('load', '<%=cp%>/rbac/getUser/'+row.id+'?rnd='+Math.random());
				form_upt.url = '<%=cp%>/rbac/editUser?id='+row.id+'&rnd='+Math.random();
				form = form_upt;
				
				$('#span_userno')[0].innerHTML = row.userno;
				$('#span_username')[0].innerHTML = row.username;
				
				//以下为 支持ie9需要给form中的select控件赋值
				$('#upt_section_id').combobox('setValue',row.section.id);
				$('#upt_role_id').combobox('setValue',row.userRoles.length>0 ? row.userRoles[0].id : "");
			} else {
				$.messager.show({
					title:'警告', 
					msg:'请您先选择用户资料。'
				});
			}
  		}
  		/* 打开查询表单窗口 */
  		function openSearchForm(){
  			var width = 320;
  			var height = 100;
  			var top = (document.body.clientHeight-height)/2;
  			var left = (document.body.clientWidth-width)/2;
			$('#searchform').window({
				title: '查询',
				width: width,
				height: height,
				top: top,
  				left: left,
				modal: false,
				closed: false,
				minimizable:false,
      			maximizable:false,
      			collapsible:false,
				resizable:false,
				iconCls:'icon-search'
			});
		}
  		/* 关闭表单窗口 */
  		function closeWindow(){
			if(win_add)
				win_add.window('close');
			if(win_upt)
				win_upt.window('close');
		}
  		/* 执行高级查询 */
  		function do_search(){
			var name = $('#keyword').val();
			$('#tt').datagrid('options').queryParams = {queryname:name};
			$('#tt').datagrid('options').pageNumber = 1;
			$('#tt').datagrid('getPager').pagination({pageNumber:1});
			$('#tt').datagrid('reload');
		}
  		/* 执行常规查询 */
  		function normalQuery(value){
  			grid.datagrid('load',{
				queryname: value
			});
  		}
  		/* 显示全部记录 */
  		function displayAll(){
  			grid.datagrid('load',{queryname:''});
  		}
  		/* 保存数据 */
  		function do_saveData(){
  			form.form('submit', {
				url:form.url,
				onSubmit:function(){  
					return $(this).form('validate');  
				},
				success:function(data){
					data = unescape(data);
					eval('data='+data);
					if (data.success){
						grid.datagrid('reload');
						closeWindow();
					} else {
						$.messager.alert('错误',data.msg,'error');
					}
				}
			});
  		}
  		/* 设置用户状态 */
  		function setupStatus(index,statVal){
  			var row = grid.datagrid('getRows')[index];
  			if(statVal=='0'){
  				stattitle = '禁用，该用户将不能登录本系统。';
  				fcolor = '#E10900';
  			}else{
  				stattitle = '启用，该用户将可以登录本系统。';
  				fcolor = '#009900';
  			}
  			$.post(
				'<%=cp%>/rbac/setUserStatus?rnd='+Math.random(),
				{uid:row.id,statVal:statVal},
				function(msg) {
					data = unescape(msg);
					eval('result='+data);
					if(result.success==1){
						$.messager.show({
							title:'提示',
							msg:'<span style="color:'+fcolor+';">已成功将<span style="font-weight:bold;">'+row.username+'</span>的状态设置为'+stattitle+'</span>',
							timeout:3000,
							showType:'fade'
						});
						grid.datagrid('reload');
					}else{
						$.messager.alert('错误',result.msg,'error');
					}
				}
			);
  		}
  	</script>
  </head>
  <body style="margin:0px;" id="c_body">
  	<!-- datagrid -->
	<table id="tt"></table>
	<input id="q" class="easyui-searchbox" data-options="searcher:normalQuery,prompt:'按用户名查询'" style="width:200px; margin:5px;"></input>
	<!-- search form -->
	<div id="searchform" class="easyui-window" closed="true" modal="false">
		<table width="100%" cellspacing="1" cellpadding="2" border="0" align="center">
		  <tr>
		    <td align="center" height="60">名称：<input type="text" id="keyword"><a class="easyui-linkbutton" icon="icon-search" href="javascript:void(0)" onclick="do_search()">查询</a></td>
		  </tr>
		</table>
	</div>
	<!-- data item form for insert -->
	<div id="userform" class="easyui-window" closed="true" modal="true">
	    <div style="text-align:center; padding:1px;">
	        <form id="userinfo_form" method="post">
	            <table width="90%" cellspacing="1" cellpadding="2" border="0" align="center">
	            	<tr>
	            		<td>分配单位：</td>
	            		<td><select class="easyui-combobox easyui-validatebox" required="true" missingMessage="请给该用户分配所在单位" id="section_id" name="section_id" data-options="url:'<%=cp%>/rbac/getSectionList',method:'get',textField:'name',valueField:'id',editable:false" style="width:100px"></select></td>
	            		<td>分配角色：</td>
	            		<td><select class="easyui-combobox easyui-validatebox" required="true" missingMessage="请给该用户分配系统角色" id="role_id" name="role_id" data-options="url:'<%=cp%>/rbac/getAllRolesList',method:'get',textField:'comments',valueField:'id',editable:false" style="width:100px"></select></td>
	            	</tr>
	                <tr>
	                    <td>用户编号：</td>
	                    <td><input type="text" name="userno" class="easyui-validatebox" required="true" missingMessage="请填写用户编号" validType="remote['<%=cp%>/rbac/chkUserProperty?chkColumn=userno&rnd='+Math.random(),'colname']" invalidMessage="该用户编号已存在" /></td>
	                    <td>用  户  名：</td>
	                    <td><input type="text" name="username" class="easyui-validatebox" required="true" missingMessage="请填写用户名" validType="remote['<%=cp%>/rbac/chkUserProperty?chkColumn=username&rnd='+Math.random(),'colname']" invalidMessage="该用户名已存在" /></td>
	                </tr>
	                <tr>
	                    <td>密　　码：</td>
	                    <td><input id="_password" type="password" name="password" class="easyui-validatebox" required="true" missingMessage="请填写密码" /></td>
	                    <td>确认密码：</td>
	                    <td><input type="password" name="password_confirm" class="easyui-validatebox" required="true" validType="equals['#_password']" missingMessage="请填写确认密码" /></td>
	                </tr>
	                <tr>
	                    <td>真实姓名：</td>
	                    <td><input type="text" name="truename" class="easyui-validatebox" required="true" missingMessage="请填写真实姓名" /></td>
	                    <td>电子邮件：</td>
	                    <td><input type="text" name="email" class="easyui-validatebox" validType="email" invalidMessage="邮箱格式错误" /></td>
	                </tr>
	                <tr>
	                    <td>住宅电话：</td>
	                    <td><input type="text" name="hometel" /></td>
	                    <td>单位电话：</td>
	                    <td><input type="text" name="unittel" /></td>
	                </tr>
	                <tr>
	                    <td>手机号码：</td>
	                    <td><input type="text" name="mobileno" /></td>
	                    <td>联系地址：</td>
	                    <td><input type="text" name="addr" /></td>
	                </tr>
	                <tr>
	                    <td>备注说明：</td>
	                    <td colspan="3"><textarea name="comments" cols="30" rows="5" style="width:100%;"></textarea></td>
	                </tr>
	            </table>
	        </form>
	    </div>
	    <div style="text-align:center;padding:5px;">
	        <a href="javascript:void(0)" onclick="do_saveData()" id="btn-save" class="easyui-linkbutton" icon="icon-save">保存</a>  
	        <a href="javascript:void(0)" onclick="closeWindow()" id="btn-cancel" class="easyui-linkbutton" icon="icon-cancel">取消</a>  
	    </div>
	</div>
	<!-- data item form for update -->
	<div id="upt_userinfo_win" class="easyui-window" closed="true" modal="true">
	    <div style="text-align:center; padding:1px;">
	        <form id="upt_userinfo_form" method="post">
	            <table width="90%" cellspacing="1" cellpadding="2" border="0" align="center">
	            	<tr>
	                    <td>用户编号：</td>
	                    <td><span id="span_userno"></span></td>
	                    <td>用  户  名：</td>
	                    <td><span id="span_username"></span></td>
	                </tr>
	            	<tr>
	            		<td>分配单位：</td>
	            		<td><select class="easyui-combobox easyui-validatebox" required="true" missingMessage="请给该用户分配所在单位" id="upt_section_id" name="section_id" data-options="url:'<%=cp%>/rbac/getSectionList',method:'get',textField:'name',valueField:'id',editable:false" style="width:100px"></select></td>
	            		<td>分配角色：</td>
	            		<td><select class="easyui-combobox easyui-validatebox" required="true" missingMessage="请给该用户分配系统角色" id="upt_role_id" name="role_id" data-options="url:'<%=cp%>/rbac/getAllRolesList',method:'get',textField:'comments',valueField:'id',editable:false" style="width:100px"></select></td>
	            	</tr>
	                <tr>
	                    <td>更新密码：</td>
	                    <td><input id="_pwd" type="password" name="password" /></td>
	                    <td>确认密码：</td>
	                    <td><input class="easyui-validatebox" type="password" validType="equals['#_pwd']" name="password_confirm" /></td>
	                </tr>
	                <tr>
	                	<td>&nbsp;</td>
	                	<td colspan="3"><span style="color:#E10900;">(如不需更改密码，请您留空上面两项)</span></td>
	                </tr>
	                <tr>
	                    <td>真实姓名：</td>
	                    <td><input type="text" name="truename" class="easyui-validatebox" required="true" missingMessage="请填写真实姓名" /></td>
	                    <td>电子邮件：</td>
	                    <td><input type="text" name="email" class="easyui-validatebox" validType="email" invalidMessage="邮箱格式错误" /></td>
	                </tr>
	                <tr>
	                    <td>住宅电话：</td>
	                    <td><input type="text" name="hometel" /></td>
	                    <td>单位电话：</td>
	                    <td><input type="text" name="unittel" /></td>
	                </tr>
	                <tr>
	                    <td>手机号码：</td>
	                    <td><input type="text" name="mobileno" /></td>
	                    <td>联系地址：</td>
	                    <td><input type="text" name="addr" /></td>
	                </tr>
	                <tr>
	                    <td>备注说明：</td>
	                    <td colspan="3"><textarea name="comments" cols="30" rows="5" style="width:100%;"></textarea></td>
	                </tr>
	            </table>
	        </form>
	    </div>
	    <div style="text-align:center;padding:5px;">
	        <a href="javascript:void(0)" onclick="do_saveData()" id="btn-save" class="easyui-linkbutton" icon="icon-save">保存</a>  
	        <a href="javascript:void(0)" onclick="closeWindow()" id="btn-cancel" class="easyui-linkbutton" icon="icon-cancel">取消</a>  
	    </div>
	</div>
  </body>
</html>